package com.cncsi.service;


import com.cncsi.dao.RestaurantRepository;
import com.cncsi.dao.UserRepository;
import com.cncsi.entity.Menu;
import com.cncsi.entity.Restaurant;
import com.cncsi.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by liuyf90 on 2018/5/6.
 */
@Service
@Transactional
public class RestaurantService {
    @Autowired
    private RestaurantRepository restaurantRepository;


    public List<Restaurant> getRestaurantList(){
        List<Restaurant> restaurantList = restaurantRepository.findAll();
        Menu menu = new Menu();
        for (Restaurant restaurant:restaurantList){
            restaurant.setMenuList(menu.cleanRelation(restaurant.getMenuList()));
        }

        return restaurantList;
    }

    public List<Restaurant> getRestaurantAll(){
        List<Restaurant> restaurantAll = restaurantRepository.findAll();
        return restaurantAll;
    }

    public String saveRestaurant(Restaurant restaurant){
        try {
            restaurantRepository.save(restaurant);
            return "success";
        }catch (Exception e){
            return "fail";
        }

    }

    public Restaurant getResaaurantForOne(Integer restaurantId){
        Restaurant restaurant = restaurantRepository.findOne(restaurantId);
        return restaurant;
    }

}